package com.ohos.carbon.slice.componentList;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.model.DemoDataBean;
import com.ohos.carbon.provider.ComponentImageProvider;
import com.ohos.carbon.slice.MainAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Image;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ElementScatter;

import java.util.ArrayList;
import java.util.Map;

import static com.ohos.carbon.slice.MainAbilitySlice.pf;

public class ImageAndTextSlice extends AbilitySlice {

    private Image back_button;
    private Text text_title, text;
    private ListContainer listContainer;
    private ArrayList<DemoDataBean> mList;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_image_and_text);
        initData();
        initView();

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("Image,text,subtext and date list item")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("Image,text,subtext and date list item")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("Image,text,subtext and date list item", integer);
                        pf.flushSync();
                    }
                }
            } else {
                pf.putInt("Image,text,subtext and date list item", 1);
                pf.flushSync();
            }
        } else {
            pf.putInt("Image,text,subtext and date list item", 1);
            pf.flushSync();
        }
    }

    private void initData() {
        mList = new ArrayList<>();
        Element favour = ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_ic_star_border_black_24dp);
        mList.add(new DemoDataBean(1, null, "Header", null, null));
        mList.add(new DemoDataBean(ResourceTable.Media_randomdata_background0, null, "Jonathan Body Evans", "Lorem ipsum dolor sit amet.", "11:21,10 Mar"));
        mList.add(new DemoDataBean(ResourceTable.Media_randomdata_background1, null, "Dennis Diaz", "Proin nibh augue susipit a.", "11:30,07 Mar"));
        mList.add(new DemoDataBean(ResourceTable.Media_randomdata_background4, null, "Eart Harry Perry", "Cars vet torem", "08:54,06 Mar"));
        mList.add(new DemoDataBean(ResourceTable.Media_randomdata_background2, null, "Kathy Amanda Diaz", "Etiam pellenteaque aliquet teilue", "05:31,11 Mar"));

        mList.add(new DemoDataBean(1, null, "Header", null, null));
        mList.add(new DemoDataBean(ResourceTable.Media_randomdata_background3, null, "Huangd nodua nujd", "Ehunsg sions hinsgd mkojs", "09:28,02 Mar"));

        mList.add(new DemoDataBean(1, null, "Header", null, null));
        mList.add(new DemoDataBean(ResourceTable.Media_randomdata_background2, null, "Muainin binsibd", "Nmnisnmd sokxhs shgjxhg sjsdc", "08:37,09 Mar"));
        mList.add(new DemoDataBean(ResourceTable.Media_randomdata_background4, null, "Mbusbvs sbuvsu sisj", "Chusngs joshd hosgijd yhh", "10:48,07 Mar"));
        mList.add(new DemoDataBean(ResourceTable.Media_randomdata_background0, null, "Vgyudb jidbd ijss sos", "Bhusnb bus hisbn ddss", "03:38,07 Mar"));

        mList.add(new DemoDataBean(1, null, "Header", null, null));
        mList.add(new DemoDataBean(ResourceTable.Media_randomdata_background1, null, "Bhusb jin jicbs", "Hgyags gysbs kpsj sssadw", "05:30,10 Mar"));
        mList.add(new DemoDataBean(ResourceTable.Media_randomdata_background2, null, "Xbudb hihd hip", "Fgsu jihd johicxjs jijs", "06:26,12 Mar"));
        mList.add(new DemoDataBean(ResourceTable.Media_randomdata_background3, null, "Aiudb husn jisni", "Chusbd ihsiubd johs kojsnb", "08:48,11 Mar"));
        mList.add(new DemoDataBean(ResourceTable.Media_randomdata_background4, null, "Yhusg jidh klpjoa", "Cguys hiugbs bjbs ais", "05:17,31 Mar"));

    }

    private void initView() {
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        text_title = (Text) findComponentById(ResourceTable.Id_title_text);
        text_title.setText("Image,text,subtext...");
//        text = (Text) findComponentById(ResourceTable.Id_text);
//        text.setText(ResourceTable.String_text);
        listContainer = (ListContainer) findComponentById(ResourceTable.Id_listview);
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        listContainer.setItemProvider(new ComponentImageProvider(mList, getContext()));
        back_button.setClickedListener(component -> onBackPressed());
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
